package com.weiyuwqj.admin.controller.admin.asset;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

import com.weiyuwqj.api.model.SysAssetCompany;
import com.weiyuwqj.api.service.SysAssetCompanyService;
import com.weiyuwqj.api.service.SysAssetPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.SysAssetDeposit;
import com.weiyuwqj.api.model.SysAssetProduct;
import com.weiyuwqj.api.model.vo.SysAssetProductVo;
import com.weiyuwqj.api.service.SysAssetDepositService;
import com.weiyuwqj.api.service.SysAssetProductService;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.IpUtils;
import com.weiyuwqj.api.util.StringUtils;

import java.util.Map;

/**
 * @author cx
 * @className:SysAssetCompanyController
 * @Description: 资产方产品controller
 * @date:2017年3月25日上午11:27:40
 */
@RestController
@RequestMapping(value = "/admin/assetProduct")
public class SysAssetProductController extends BaseController {

    @Autowired
    private SysAssetProductService sysAssetProductService;
    @Autowired
    private SysAssetDepositService sysAssetDepositService;
    @Autowired
    private SysAssetCompanyService sysAssetCompanyService;
    @Autowired
    private SysAssetPictureService sysAssetPictureService;

    /**
     * 列表分页
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    public PageList getPageList(final Paginator p, final String name, final Integer companyId) {
        final HashMap<String, Object> map = new HashMap<String, Object>();
        Integer assetId = (Integer) getSession().getAttribute("assetId");
        if (!StringUtils.isNull(name)) {
            map.put("name", name);
        }
        if (companyId != null && companyId > 0) {
            map.put("companyId", companyId);
        }
        if (assetId != null && assetId > 0) {
            map.put("assetId", assetId);
        }
        return this.sysAssetProductService.getPageList(p, map);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public SysAssetProductVo findById(final Integer id) {
        final SysAssetProductVo assetProductVo = this.sysAssetProductService.findById(id);
        if (assetProductVo == null) {
            throw new CrowdException("sys.asset.product.not_exists");
        }
        assetProductVo.setInterestTime(String.valueOf(
                DateUtils.getDateYYYYMMdd(assetProductVo.getInterestTime()).getTime()));
        final SysAssetDeposit sysAssetDeposit = this.sysAssetDepositService.findByCompanyId(assetProductVo.getCompanyId());
        if (sysAssetDeposit != null) {
            assetProductVo.setDepositAmount(sysAssetDeposit.getTotalAmount() - sysAssetDeposit.getUsedAmount());
        }


        return assetProductVo;
    }

    /**
     * 保存
     *
     * @param sysAssetProduct
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public HashMap<String, String> save(final SysAssetProductVo sysAssetProduct) {
        //找到资产方ID
        Integer assetId = (Integer) getSession().getAttribute("assetId");
        SysAssetCompany sysAssetCompany = sysAssetCompanyService.selectByKey(assetId);
        sysAssetProduct.setCompanyName(sysAssetCompany.getName());
        sysAssetProduct.setCompanyId(sysAssetCompany.getId());
        if (sysAssetProduct.getItemId() > 0) {
            throw new CrowdException("sys.asset.product.asset_package.not_edit");
        }
        sysAssetProduct.validData();
        sysAssetProduct.setAddip(IpUtils.get());
        sysAssetProduct.setAddtime(new Date());
        sysAssetProduct.setCreator(getSessionUserId());
        this.sysAssetProductService.saveAssetProduct(sysAssetProduct);
        return Message.OK;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HashMap<String, String> delete(final Integer id) {
        final SysAssetProduct sysAssetProduct = this.findById(id);
        if (sysAssetProduct.getItemId() > 0) {
            throw new CrowdException("sys.asset.product.asset_package.not_detele");
        }
        sysAssetPictureService.deleteByProductId(id);
        this.sysAssetProductService.delete(id);
        return Message.OK;
    }


    /**
     * 资产合成标的
     *
     * @param
     * @param productId 产品id多个以‘，’号隔开
     * @return
     */
    @RequestMapping(value = "/pushAssetPackage", method = RequestMethod.POST)
    public HashMap<String, String> pushAssetPackage(final Integer productId, final Integer debtTransferable, final String endTime, Integer itemIsrecommend,
                                                    Integer itemIsnew, BigDecimal itemRate, String riskControlMeasures, String interestTime, BigDecimal debtServeFeeRate,
                                                    BigDecimal itemSingleMinInvestment) {
        Map map = new HashMap();
        map.put("itemIsrecommend", itemIsrecommend);
        map.put("itemIsnew", itemIsnew);
        map.put("riskControlMeasures", riskControlMeasures);
        map.put("interestTime", interestTime);
        map.put("itemSingleMinInvestment", itemSingleMinInvestment);
        this.sysAssetProductService.pushAssetPackage(productId, debtTransferable, DateUtils.getDateYYYYMMdd(endTime), map, debtServeFeeRate,itemRate);
        return Message.OK;
    }

}
